<template>
  <div>
    <van-field v-model="times_parent" clickable readonly @click="showPicker = true">
      <template #button>
        <van-button size="small" round @click="setTime($event)" class="setbtn">{{$t('Save')}}</van-button>
      </template>
    </van-field>
    <van-popup v-model="showPicker" round position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="datetime"
        @cancel="showPicker = false"
        @confirm="onConfirm"
        :confirm-button-text="$t('OK')"
        :cancel-button-text="$t('Cancel')"
      />
    </van-popup>
  </div>
</template>

<script>
import moment from "moment";
export default {
  props: ["times"],
  data() {
    return {
      showPicker: false,
      currentDate: "",
      times_parent: this.times,
    };
  },
  watch: {
    times(newVal) {
      this.times_parent = this.times;
    },
  },
  methods: {
    onConfirm() {
      this.showPicker = false;
      this.times_parent = moment(this.currentDate).format("YYYY-MM-DD HH:mm");
    },
    setTime(e) {
      e.stopPropagation();
      let timeArr = this.times_parent.split(" ");
      let timeArrA = timeArr[0].split("-");
      let timeArrB = timeArr[1].split(":");
      let formData = {
        year: timeArrA[0],
        month: timeArrA[1],
        day: timeArrA[2],
        hour: timeArrB[0],
        minute: timeArrB[1],
      };
      this.comm.bdSetData("RTC", formData);
    },
  },
};
</script>

<style scoped lang="less">
</style>